package br.com.dallasmotopecas.util.teste;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class Agendamentos implements ServletContextListener
{
	private static int count = 0;
	Timer timer = null;
	
	/*private void primeiraTarefa()
	{
		final Timer t = new Timer();
		t.schedule(new TimerTask()
		{
			
			@Override
			public void run()
			{
				System.out.println("Contador " + (++count) + "Time: " + new Date());
				new Execucoes().tabuada(count);
				if (count == 5)
					t.cancel();
				
			}
		}, 5000, 3000);
	}*/
	
	public void terceiraTarefa(ServletContextEvent sce)
	{
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 50);
		c.set(Calendar.SECOND, 0);
		
		Date time = c.getTime();
		
		final Timer t = new Timer();
		t.schedule(new TimerTask()
		{
			
			@Override
			public void run()
			{
				System.out.println("Contador " + (++count) + "Time: " + new Date());
				new Execucoes().tabuada(count);
					//t.cancel();
				
			}
		}, time);
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0)
	{
		if (timer != null)
			timer.cancel();
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0)
	{
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 18);
		c.set(Calendar.MINUTE, 50);
		c.set(Calendar.SECOND, 0);
		
		Date time = c.getTime();
		
		timer = new Timer();
		timer.schedule(new TimerTask()
		{
			
			@Override
			public void run()
			{
				System.out.println("Contador " + (++count) + "Time: " + new Date());
				new Execucoes().tabuada(count);
					//t.cancel();
				
			}
		}, time);
	}
}
class Execucoes
{
	public void tabuada(int valor)
	{
		for (int i = 0; i < 11; i++)
		{
			System.out.println(i + " * " + valor + " = " + (i * valor));
		}
	}
}
